home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / SCROLLBA.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.6 KB  |  418 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1991, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.8  $
  6. //
  7. // Implementation of class TScrollBar.  This defines the basic behavior of all
  8. // scrollbar controls.
  9. //----------------------------------------------------------------------------
  10. #pragma hdrignore SECTION
  11. #include <owl/pch.h>
  12. #if !defined(OWL_SCROLLBA_H)
  13. # include <owl/scrollba.h>
  14. #endif
  15. #if !defined(WINSYS_UIMETRIC_H)
  16. # include <winsys/uimetric.h>
  17. #endif
  18.  
  19. OWL_DIAGINFO;
  20.  
  21. #if !defined(SECTION) || SECTION == 1
  22.  
  23. DEFINE_RESPONSE_TABLE1(TScrollBar, TControl)
  24.   EV_WM_VSCROLL,
  25.   EV_WM_HSCROLL,
  26. END_RESPONSE_TABLE;
  27.  
  28. //
  29. // Constructor for a TScrollBar object
  30. //
  31. // If the size attribute (H for horizontal scrollbars, W for vertical) is
  32. // zero, the attribute is set to the appropriate system metric
  33. //
  34. TScrollBar::TScrollBar(TWindow*        parent,
  35.                        int             id,
  36.                        int x, int y, int w, int h,
  37.                        bool            isHScrollBar,
  38.                        TModule*        module)
  39. :
  40.   TControl(parent, id, 0, x, y, w, h, module)
  41. {
  42.   LineMagnitude = 1;
  43.   PageMagnitude = 10;
  44.  
  45.   if (isHScrollBar) {
  46.     Attr.Style |= SBS_HORZ;
  47.  
  48.     if (Attr.H == 0)
  49.       Attr.H = TUIMetric::CyHScroll;
  50.   }
  51.   else {
  52.     Attr.Style |= SBS_VERT;
  53.  
  54.     if (Attr.W == 0)
  55.       Attr.W = TUIMetric::CxVScroll;
  56.   }
  57. }
  58.  
  59. //
  60. // Constructor for a scrollbar from resource (dialog child)
  61. // The LineMagnitude is set to 1 by default.
  62. // The PageMagnitude is set to 10 by default.
  63. //
  64. TScrollBar::TScrollBar(TWindow*   parent,
  65.                        int        resourceId,
  66.                        TModule*   module)
  67. :
  68.   TControl(parent, resourceId, module)
  69. {
  70.   LineMagnitude = 1;
  71.   PageMagnitude = 10;
  72. }
  73.  
  74. //
  75. // Transfers state information for a TScrollbar
  76. //
  77. // Direction specifies whether data is to be read from or written to the
  78. // buffer, or whether the data element size is simply to be returned
  79. //
  80. // the return value is the size (in bytes) of the transfer data
  81. //
  82. uint
  83. TScrollBar::Transfer(void* buffer, TTransferDirection direction)
  84. {
  85.   TScrollBarData* scrollBuff = (TScrollBarData*)buffer;
  86.  
  87.   if (direction == tdGetData) {
  88.     GetRange(scrollBuff->LowValue, scrollBuff->HighValue);
  89.     scrollBuff->Position = GetPosition();
  90.   }
  91.   else if (direction == tdSetData) {
  92.     SetRange(scrollBuff->LowValue, scrollBuff->HighValue);
  93.     SetPosition(scrollBuff->Position);
  94.   }
  95.  
  96.   return sizeof(TScrollBarData);
  97. }
  98.  
  99. //
  100. // Return name of predefined Windows scrollbar class
  101. //
  102. char far *
  103. TScrollBar::GetClassName()
  104. {
  105.   return "SCROLLBAR";
  106. }
  107.  
  108. //
  109. // Initialize the scrollbar to the default range of 0 .. 100
  110. //
  111. void
  112. TScrollBar::SetupWindow()
  113. {
  114.   SetRange(0, 100);
  115.   TControl::SetupWindow();
  116. }
  117.  
  118. #if defined(BI_PLAT_WIN32)
  119. //
  120. // Set scrollbar info
  121. //
  122. void
  123. TScrollBar::SetScrollInfo(SCROLLINFO* info, bool redraw)
  124. {
  125.   PRECONDITION(GetHandle());
  126.   ::SetScrollInfo(GetHandle(), SB_CTL, info, redraw);
  127. }
  128.  
  129. //
  130. // Retrieve the scroll info.
  131. //
  132. void
  133. TScrollBar::GetScrollInfo(SCROLLINFO* info) const
  134. {
  135.   PRECONDITION(GetHandle());
  136.   ::GetScrollInfo(GetHandle(), SB_CTL, info);
  137. }
  138. #endif
  139.  
  140. //
  141. // Return the current position of the thumb.
  142. //
  143. int
  144. TScrollBar::GetPosition() const
  145. {
  146.   PRECONDITION(GetHandle());
  147. #if defined(BI_PLAT_WIN16)
  148.   return ::GetScrollPos(GetHandle(), SB_CTL);
  149. #else
  150.   SCROLLINFO info;
  151.   ZeroMemory(&info, sizeof info);
  152.   info.cbSize = sizeof info;
  153.   info.fMask = SIF_POS;
  154.   GetScrollInfo(&info);
  155.   return info.nPos;
  156. #endif
  157. }
  158.  
  159. //
  160. // Sets the position of the thumb.
  161. //
  162. void
  163. TScrollBar::SetPosition(int thumbPos, bool redraw)
  164. {
  165.   PRECONDITION(GetHandle());
  166.   int  min, max;
  167.   GetRange(min, max);
  168.  
  169.   // Constrain "thumbPos" to be in the range "min .. max"
  170.   //
  171.   if (thumbPos > max)
  172.     thumbPos = max;
  173.  
  174.   else if (thumbPos < min)
  175.     thumbPos = min;
  176.  
  177.   if (thumbPos != GetPosition()) {
  178. #if defined(BI_PLAT_WIN16)
  179.     ::SetScrollPos(GetHandle(), SB_CTL, thumbPos, redraw);
  180. #else
  181.     SCROLLINFO info;
  182.     ZeroMemory(&info, sizeof info);
  183.     info.cbSize = sizeof info;
  184.     info.fMask = SIF_POS;
  185.     info.nPos = thumbPos;
  186.     SetScrollInfo(&info, redraw);
  187. #endif
  188.   }
  189. }
  190.  
  191. //
  192. // Return the current delta to move the thumb when page up/page down is
  193. // received.
  194. //
  195. int
  196. TScrollBar::GetPageMagnitude() const
  197. {
  198.   return PageMagnitude;
  199. }
  200.  
  201. //
  202. // Set the delta to move the thumb when page up/page down is received.
  203. //
  204. void
  205. TScrollBar::SetPageMagnitude(int pagemagnitude)
  206. {
  207.   PageMagnitude = pagemagnitude;
  208. #if defined(BI_PLAT_WIN32)
  209.   PRECONDITION(GetHandle());
  210.   SCROLLINFO info;
  211.   ZeroMemory(&info, sizeof info);
  212.   info.cbSize = sizeof info;
  213.   info.fMask = SIF_PAGE;
  214.   info.nPage = pagemagnitude;
  215.   SetScrollInfo(&info);
  216. #endif
  217. }
  218.  
  219. //
  220. // Return the range of the scrollbar.
  221. //
  222. void
  223. TScrollBar::GetRange(int& min, int& max) const
  224. {
  225.   PRECONDITION(GetHandle());
  226. #if defined(BI_PLAT_WIN16)
  227.   ::GetScrollRange(GetHandle(), SB_CTL, &min, &max);
  228. #else
  229.   SCROLLINFO info;
  230.   ZeroMemory(&info, sizeof info);
  231.   info.cbSize = sizeof info;
  232.   info.fMask = SIF_RANGE;
  233.   GetScrollInfo(&info);
  234.   min = info.nMin;
  235.   max = info.nMax;
  236. #endif
  237. }
  238.  
  239. //
  240. // Sets the range of the scrollbar.
  241. //
  242. void
  243. TScrollBar::SetRange(int min, int max, bool redraw)
  244. {
  245.   PRECONDITION(GetHandle());
  246. #if defined(BI_PLAT_WIN16)
  247.   ::SetScrollRange(GetHandle(), SB_CTL, min, max, redraw);
  248. #else
  249.   SCROLLINFO info;
  250.   ZeroMemory(&info, sizeof info);
  251.   info.cbSize = sizeof info;
  252.   info.fMask = SIF_RANGE;
  253.   info.nMin = min;
  254.   info.nMax = max;
  255.   SetScrollInfo(&info,redraw);
  256. #endif
  257. }
  258.  
  259.  
  260. //
  261. // Changes the position of the thumb by "delta" and returns the new position
  262. //
  263. int
  264. TScrollBar::DeltaPos(int delta)
  265. {
  266.   if (delta != 0)
  267.     SetPosition(GetPosition() + delta);
  268.  
  269.   return GetPosition();
  270. }
  271.  
  272. //
  273. // Changes the position of the thumb by "LineMagnitude"
  274. //
  275. void
  276. TScrollBar::SBLineUp()
  277. {
  278.   DeltaPos(-LineMagnitude);
  279. }
  280.  
  281. //
  282. // Changes the position of the thumb by "LineMagnitude"
  283. //
  284. void
  285. TScrollBar::SBLineDown()
  286. {
  287.   DeltaPos(LineMagnitude);
  288. }
  289.  
  290. //
  291. // Changes the position of the thumb by "PageMagnitude"
  292. //
  293. void
  294. TScrollBar::SBPageUp()
  295. {
  296.   DeltaPos(-PageMagnitude);
  297. }
  298.  
  299. //
  300. // Changes the position of the thumb by "PageMagnitude"
  301. //
  302. void
  303. TScrollBar::SBPageDown()
  304. {
  305.   DeltaPos(PageMagnitude);
  306. }
  307.  
  308. //
  309. // Moves the thumb to the new position
  310. //
  311. void
  312. TScrollBar::SBThumbPosition(int thumbPos)
  313. {
  314.   SetPosition(thumbPos);
  315. }
  316.  
  317. //
  318. // Moves the thumb to the new position
  319. //
  320. void
  321. TScrollBar::SBThumbTrack(int thumbPos)
  322. {
  323.   SetPosition(thumbPos);
  324. }
  325.  
  326. //
  327. // Moves the thumb to the top of the scrollbar
  328. //
  329. void
  330. TScrollBar::SBTop()
  331. {
  332.   int  min, max;
  333.   GetRange(min, max);
  334.   SetPosition(min);
  335. }
  336.  
  337. //
  338. // Moves the thumb to the bottom of the scrollbar
  339. //
  340. void
  341. TScrollBar::SBBottom()
  342. {
  343.   int  min, max;
  344.   GetRange(min, max);
  345.   SetPosition(max);
  346. }
  347.  
  348. //
  349. // User released the mouse after scrolling
  350. //
  351. void
  352. TScrollBar::SBEndScroll()
  353. {
  354. }
  355.  
  356. //
  357. // Horizontal message handler.
  358. // Dispatch messages to virtual functions.
  359. //
  360. void
  361. TScrollBar::EvHScroll(uint scrollCode, uint thumbPos, THandle /*hCtl*/)
  362. {
  363.   switch (scrollCode) {
  364.     case SB_LINEDOWN:      SBLineDown(); break;
  365.     case SB_LINEUP:        SBLineUp(); break;
  366.     case SB_PAGEDOWN:      SBPageDown(); break;
  367.     case SB_PAGEUP:        SBPageUp(); break;
  368.     case SB_TOP:           SBTop(); break;
  369.     case SB_BOTTOM:        SBBottom(); break;
  370.     case SB_THUMBPOSITION: SBThumbPosition(thumbPos); break;
  371.     case SB_THUMBTRACK:    SBThumbTrack(thumbPos); break;
  372.     case SB_ENDSCROLL:     SBEndScroll();
  373.   }
  374. }
  375.  
  376. //
  377. // Forward vertical message to horizontal message handler.
  378. //
  379. void
  380. TScrollBar::EvVScroll(uint scrollCode, uint thumbPos, THandle hCtl)
  381. {
  382.   EvHScroll(scrollCode, thumbPos, hCtl);
  383. }
  384.  
  385. #endif
  386. #if !defined(SECTION) || SECTION == 2
  387.  
  388. IMPLEMENT_STREAMABLE1(TScrollBar, TControl);
  389.  
  390. #if !defined(BI_NO_OBJ_STREAMING)
  391.  
  392. //
  393. // Reads an instance of TScrollBar from the passed ipstream.
  394. //
  395. void*
  396. TScrollBar::Streamer::Read(ipstream& is, uint32 /*version*/) const
  397. {
  398.   ReadBaseObject((TControl*)GetObject(), is);
  399.   is >> GetObject()->LineMagnitude
  400.      >> GetObject()->PageMagnitude;
  401.   return GetObject();
  402. }
  403.  
  404. //
  405. // Writes the TScrollBar to the passed opstream.
  406. //
  407. void
  408. TScrollBar::Streamer::Write(opstream& os) const
  409. {
  410.   WriteBaseObject((TControl*)GetObject(), os);
  411.   os << GetObject()->LineMagnitude
  412.      << GetObject()->PageMagnitude;
  413. }
  414.  
  415. #endif  // if !defined(BI_NO_OBJ_STREAMING)
  416.  
  417. #endif
  418.